package com.zucc.eye.util;

import com.alibaba.fastjson.JSONObject;
//import com.sun.org.slf4j.internal.Logger;
//import com.sun.org.slf4j.internal.LoggerFactory;
import com.zucc.eye.okhttp.StateCallBack;
//import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONUtil;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;

@Component
@Slf4j
public class HttpUtil {



    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    //static OkHttpClient client = new OkHttpClient();
//    static OkHttpClient client;

//    public static OkHttpClient getClient() {
//        if (client == null) {
//            return new OkHttpClient();
//        }
//        return client;
//    }

//    public static JsonObject strToJson(String string) {
//        Gson gson = new Gson();
//        JsonObject jsonObject = gson.fromJson(string, JsonObject.class);
//        return jsonObject;
//    }


//    public static HTTP getHttp(String url) {
//        HTTP http = HTTP.builder()
//                .baseUrl(url)
//                .addMsgConvertor(new GsonMsgConvertor())
//                .build();
//        return http;
//    }

//    void d(String  baseUrl){
//        HTTP http = HTTP.builder()
////                .baseUrl("http://api.tianapi.com/lajifenlei/index?key="+keyRubbish+"&word=")
////                .baseUrl(Bus.tianapiBaseUrl)
//                .baseUrl(baseUrl)
//                .addMsgConvertor(new GsonMsgConvertor())
//                .build();
//
//
////
////        HTTP http = HTTP.builder()
//////                .baseUrl("http://api.tianapi.com/lajifenlei/index?key="+keyRubbish+"&word=")
////                .baseUrl("http://api.tianapi.com/lajifenlei/index")
////                .addMsgConvertor(new GsonMsgConvertor())
////                .build();
//
////        http://localhost:8889/tbAdmin/find?id=1
//
////        List<User> users = http.sync("/users") // http://api.example.com/users
////                .addPathPara("key",keyRubbish)
////                .addPathPara("word",searchWord)
////                .post()                         // GET请求
////                .getBody()                     // 获取响应报文体
////                .toList(User.class);           // 得到目标数据
//
////        Mapper mapper = http.sync("/users") // http://api.example.com/users
////                .addPathPara("key", keyRubbish)
////                .addPathPara("word", searchWord)
////                .post()                         // GET请求
////                .getBody().toMapper();
////        Mapper 可以直接映射为实体吗 okhttps
////        Mapper mapper = http.sync("/find") // http://api.example.com/users
////                .addPathPara("id", 1)
////                .post()                         // GET请求
////                .getBody().toMapper();
//
////        Mapper mapper = http.sync("") // http://api.example.com/users
//////                .addPathPara("id", 1)
////                .post()                         // GET请求
////                .getBody().toMapper();
//
////        Mapper mapper = http.sync("") // http://api.example.com/users
//////                .addPathPara("id", 1)
////                .get()                        // GET请求
////                .getBody().toMapper();
////        okhhtps 没有反应
//
////        String s = http.sync("") // http://api.example.com/users
//////                .addPathPara("id", 1)
////                .get()                        // GET请求
////                .getBody().toString();
//
//        //Object keyRubbish;
//        String res = http.sync("") // http://api.example.com/users
//                .addPathPara(Bus.APIKEY_TOKEN, Bus.keyRubbish)
//                .addPathPara(Bus.WORD_TOKEN, searchWord)
//                .get()                        // GET请求
//                .getBody().toString();
////        HttpUtil
//
//        Log.i("res",res);
//        Gson gson = new Gson();
//        JsonObject jsonObject = gson.fromJson(res, JsonObject.class);
//    }

    //public void setClient(OkHttpClient client) {
    //    this.client = client;
    //}
//    可以用

    /**
     * @param url
     * @param jsonBody 是放在 body 里面的
     * @return
     * @throws IOException
     */
//    public static String post(String url, String jsonBody) throws IOException {
    public  String post(String url, String jsonBody) throws IOException {
//        OkHttpClient client = new OkHttpClient();
//        RequestBody body = RequestBody.create(JSON, jsonBody);
//        Request request = new Request.Builder()
//                .url(url)
//                .post(body)
//                .build();
////同步
//        Response response = client.newCall(request).execute();
////        ResponseBody body1 = response.body();
////        body1.
////        ResponseBody 是json
//        if (response.isSuccessful()) {
//            return response.body().string();
//        } else {
//            throw new IOException("Unexpected code " + response);
//        }

//        OkHttpClient client = getClient();
//        client
        return post(url, jsonBody, client);
    }

//    okhttp  get
//    public static String post(String url, String jsonBody, OkHttpClient client) throws IOException {
    public  String post(String url, String jsonBody, OkHttpClient client) throws IOException {
        //OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(JSON, jsonBody);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
//同步
        Response response = client.newCall(request).execute();
//        ResponseBody body1 = response.body();
//        body1.
//        ResponseBody 是json
        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }

//    public static <T> JSONObject post(String url, T t) throws IOException {
    public  <T> JSONObject post(String url, T t) throws IOException {
//        Gson gson = new Gson();

//        RubbishInfo rubbishInfo=new RubbishInfo();
//        rubbishInfo.setQueryRub("string");

//        String jsonString = gson.toJson(t);
//        JSONUtil.
//        JSONObject.toJSONString()
        String jsonString = JSONObject.toJSONString(t);
        String res = post(url, jsonString);
//        String 转化为 JSONObject alibaba
//        https://blog.csdn.net/u011008029/article/details/51315339
//        JSONObject jsonObject1 = JSONObject.parseObject(res,JSONObject.class);
        JSONObject jsonObject1 = JSONObject.parseObject(res);
//        System.out.println(jsonString);
//        String post = httpUtil.post(listUrl, jsonString);
//        content
//        Log.d("res", "post: res  " + res);
//        JsonObject jsonObject = gson.fromJson(res, JsonObject.class);
//        JsonObject jsonObject = gson.fromJson(res, JsonObject.class);
        return jsonObject1;
////        OkHttpClient get
    }


//
//    @Value("${httputil.postimgurl}")
//    private String postimgurl;

    private OkHttpClient client = null;
    private RequestBody requestBody = null;
    private  Request request = null;


//    private Logger log = LoggerFactory.getLogger(this.getClass());
    @PostConstruct
    public void init(){
        client = new OkHttpClient();
        log.info("初始化Okhttp");
    }

    public void postTakeImg(String base64Img,final StateCallBack back,String  postImgUrl) {

        requestBody = new FormBody.Builder()
                .add("base64Str", base64Img)
                .build();

        request = new Request.Builder()
                .url(postImgUrl)
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
                back.onFail(e.toString());
            }
            @Override
            public void onResponse(okhttp3.Call call, Response response) throws IOException {
                String data = response.body().string();
//                response.body().
//                Response  body okhttp 获取 json
                back.onSuccess(data);
            }
        });
    }
}
